/**
 @Author Zhenglinbin
 @Date 2021/4/17
 **/
import { post } from '@/request/useRequest'
import {ResponseBody} from "@/api/typing";
import {Props} from "@/components/typing";
import {baseUrl} from "@/setting";

export async function deleteAll(data: any,params: Props): Promise<ResponseBody> {
    return post('role/deleteAll', data,params);
}
export async function formSubmit (url: string,params: Props,method = 'POST') {
    const form = generateElement('form')
    form.method = method
    form.action = baseUrl + url;
    buildParams(form,params)
    form.style.display = 'none'
    document.body.appendChild(form)
    form.submit()
    form.remove()
}
const buildParams = (form: any,params: Props) => {
    if(params && Object.keys(params).length > 0) {
        for(let item in params) {
            const input = generateElement('input')
            input.name = item
            input.type = 'hidden'
            if(typeof params[item] === 'object' || Array.isArray(params[item])) {
                if(item === 'filter') {
                    if(!params[item]) params[item] = {}
                    params[item]['factory'] = params.currentFactory
                }
                input.value = JSON.stringify(params[item])
            } else {
                input.value = params[item]
            }
            form.appendChild(input)
        }
    }
}
const generateElement = (name: string): any => {
    const form: any = document.createElement(name)
    if(name === 'form') {
        form.target = '_blank'
    }
    return form
}